package me.xiaoyan.android.tuition.activity;

import java.util.HashMap;
import java.util.Map;

import me.xiaoyan.android.tuition.R;
import me.xiaoyan.android.tuition.utils.AppManager;
import me.xiaoyan.android.tuition.utils.SESHttp;
import me.xiaoyan.android.tuition.utils.TuitionException;
import me.xiaoyan.android.widget.BaseUtil;
import me.xiaoyan.android.widget.DataBackListener;
import me.xiaoyan.android.widget.YLog;
import me.xiaoyan.json.JSONArray;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class Login extends Common implements OnClickListener {
	private static final String TAG = "Activity.Login";
	private TextView txtUserName;
	private TextView txtPassWord;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		txtUserName = (TextView) findViewById(R.id.txtbox_login_username);
		txtPassWord = (TextView) findViewById(R.id.txtbox_login_pwd);
		findViewById(R.id.btn_login).setOnClickListener(this);
		findViewById(R.id.txt_common_reg).setVisibility(View.VISIBLE);
		clickGoto(R.id.txt_common_reg, Register.class);

		showBackButton();
	}

	@Override
	public void onBackPressed() {
		super.goback();
	}

	@Override
	protected void onResume() {
		if (AppManager.getInstance().hasAppDatas(CUSTOMER_ID)) {
			goback();
		}

		super.onResume();
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.btn_login) {
			if (checkTextBoxNull(txtUserName,
					getString(R.string.username_is_null))
					&& checkTextBoxNull(txtPassWord,
							getString(R.string.password_is_null))) {
				if (!BaseUtil.isPhoneNumber(txtUserName.getText().toString())) {
					showToast(getString(R.string.tap_right_phone),
							Toast.LENGTH_SHORT);
					return;
				}
				Map<String, Object> param = new HashMap<String, Object>();

				param.put("loginAccount", txtUserName.getText().toString());
				param.put("pwd", txtPassWord.getText().toString());
				param.put("verifyCode", "1234");
				final ProgressDialog pd = getProgress(
						getString(R.string.loading_login_data), false);
				SESHttp.sendRequest(USER_LOGIN, param, new DataBackListener() {

					@Override
					public void success(String source) {
						pd.dismiss();
						System.out.println("DataBackListener" + source);
						try {
							JSONArray json = checkSESData(source);
							if (json.length() > 0) {
								String customerid = json.getJSONObject(0)
										.optString("customerId");
								AppManager.getInstance().saveAppDatas(
										CUSTOMER_ID, customerid);
								try {
									saveUserLoginInfo();
								} catch (Exception e) {
								}
								Intent data = new Intent();
								data.putExtra(CUSTOMER_ID, customerid);
								gobackWithResult(LOGIN_SUCCESS_BACK, data);
							} else {
								showToast(
										getString(R.string.not_found_user_data),
										Toast.LENGTH_SHORT);
							}
						} catch (Exception e) {
							showToast(e.getMessage(), Toast.LENGTH_SHORT);
						}
					}

					@Override
					public void failed(TuitionException e) {
						pd.dismiss();
						YLog.e(TAG, e);
					}
				});
			}
		}
	};

	private void saveUserLoginInfo() {
		SharedPreferences settings = getSharedPreferences(APP_DATA_KEY,
				Context.MODE_PRIVATE);
		Editor editor = settings.edit();
		editor.putString(APP_DATA_PHONE, txtUserName.getText().toString());
		editor.putString(APP_DATA_PASS, txtPassWord.getText().toString());
		editor.commit();
	}
}
